/*
 * WelfareMgr.h
 *
 *  Created on: Nov 29, 2017
 *      Author: gaoyi
 */

#pragma once

#include "base/core/singleton.h"
#include "base/core/object_pool.h"
#include "Common/DeclareProperty.h"
#include "Common/TableData/TargetTargetCfg.h"
#include "Common/TableData/TargetNormalCfg.h"
#include "Common/TableData/TargetLoginCfg.h"
#include "Common/TableData/TargetSaleCfg.h"
#include "Common/TableData/TargetDuplicateCfg.h"
#include "Common/Timer/TimerAxis.h"
#include <map>

enum OperateActivityType
{
	LOCKLY_TURN_TABLE_TYPE = 1,		//幸运转盘
	REPEAT_COST_TYPE = 2,		//重复消费
	ZHAN_LI_PAI_MING_TYPE = 3,		//战力排名
	LEVEL_PAI_MING_TYPE = 4,		//等级排名
	ACCUMULATION_COST_TYPE = 5,		//累计消费 Accumulation
	LEVEL_GIFT_TYPE = 6,		//等级礼包
	ONLINE_GIFT_TYPE = 7,		//在线礼包
	SEVEN_DAY_SIGN_TYPE = 8,		//七日签到
	EVERY_DAY_SGN_TYPE = 9,		//每日签到
	SPECIAL_DROP_TYPE = 10,		//特殊掉落
	SPECIAL_EXCHANGE_TYPE = 11,//特殊兑换
	GROWTH_FUND_TYPE = 12, //成长基金
	SINGLE_PEN_RECHARGE_TYPE = 13, //单笔充值
	ACCUMULATION_RECHARGE_TYPE = 14, //累计充值
	CONTINUE_RECHARGE_TYPE = 15, //连冲豪礼
	MONTH_CARD_TYPE = 16,
	EVERY_DAY_RECHARGE_TYPE = 17,	//每日充值
	SUPER_VALUE_GIFT_BAG = 18, //超值礼包xian
	LIMITS_RECHARGE_GIFT_BAG = 19, //限定礼包
	FIRST_RECHARGE_GIFT = 20, //首次充值
	TODAY_LIMITS_RECHARGE_GIFT_BAG = 21, //今日限定
	WORLD_MASTER_TYPE = 22, //世界大师
	POINT_TURN_TABLE_TYPE = 23, //积分转盘
};

class RangeInt
{
public:
	RangeInt(): mMinValue(0), mMaxValue(0), mBIsValid(false) { }
	RangeInt(const int32_t min, const int32_t max) : mMinValue(min), mMaxValue(max), mBIsValid(true) { }
	void CopyFrom(const RangeInt& range)
	{
		mMinValue = range.mMinValue;
		mMaxValue = range.mMaxValue;
		mBIsValid = range.mBIsValid;
	}
	RangeInt(const RangeInt& range)
	{
		if (this == &range) return;
		CopyFrom(range);
	}
	RangeInt& operator=(const RangeInt& range)
	{
		if (this == &range) return *this;
		CopyFrom(range);
		return *this;
	}
public:
	// 判断一个整数是否在这个范围中
	bool IsInRange(const int value) const
	{
		return (!mBIsValid) || (value >= mMinValue && value <= mMaxValue);
	}

	bool IsLessRange(const int value) const
	{
		return (!mBIsValid) || (value <= mMinValue);
	}

	bool IsBiggerRange(const int value) const
	{
		return (!mBIsValid) || (value >= mMaxValue);
	}

public:
	DECLARE_PROPERTY_INT32(MinValue);		//范围下界
	DECLARE_PROPERTY_INT32(MaxValue);		//范围上界
	DECLARE_PROPERTY_BOOL(BIsValid);		//该范围是否已初始化
};

// 活动条件单元
class OperateActCondition
{
public:
	OperateActCondition() { }
	OperateActCondition(RangeInt range) : mFaculty(range) { }
	void CopyFrom(const OperateActCondition& cond)
	{
		mFaculty = cond.mFaculty;
	}
	OperateActCondition(const OperateActCondition& cond)
	{
		if (this == &cond) return;
		CopyFrom(cond);
	}

	OperateActCondition& operator=(const OperateActCondition& cond)
	{
		if (this == &cond) return *this;
		CopyFrom(cond);
		return *this;
	}
public:
	DECLARE_PROPERTY_OBJECT(RangeInt, Faculty);		//正确的修为值范围
};

class OperateActTime
{
public:
	DECLARE_PROPERTY_OBJECT(RangeInt, Time);		// 有效时间范围(每天的有效时间)
public:
};

// 活动起止时间
typedef std::vector<OperateActTime> ActTimeList;

class WelfareConfigItem
{
public:
	WelfareConfigItem()
	{

	}

	void CopyFrom(const WelfareConfigItem& item)
	{
		mItemId = item.mItemId;
		mActCondition = item.mActCondition;
		mRewardId = item.mRewardId;
		mItemType = item.mItemType;
		mTiaoJian = item.mTiaoJian;
		mExtBoxId = item.mExtBoxId;
		mSonId = item.mSonId;
	}

	WelfareConfigItem(const WelfareConfigItem& item)
	{
		if (this == &item) return;
		CopyFrom(item);
	}

	WelfareConfigItem& operator=(const WelfareConfigItem& item)
	{
		if (this == &item)
		{
			return *this;
		}
		CopyFrom(item);
		return *this;
	}
public:
	DECLARE_PROPERTY_UINT32(ItemId);		//id
	DECLARE_PROPERTY_UINT32(ItemType);		//运营活动类型
	DECLARE_PROPERTY_OBJECT(OperateActCondition, ActCondition); //条件限制
	DECLARE_PROPERTY_UINT32(RewardId);
	DECLARE_PROPERTY_STRING(TiaoJian);
	DECLARE_PROPERTY_UINT32(ExtBoxId);
	DECLARE_PROPERTY_UINT32(SonId);
};

class OperateActConfigItem
{
public:
	typedef std::vector<WelfareConfigItem*>	vecWelfareItem;
public:
	DECLARE_PROPERTY_UINT32(ActId);		// 活动id
	DECLARE_PROPERTY_UINT32(ActType);
	DECLARE_PROPERTY_BOOL(IsOpen);		// 是否开启
	DECLARE_PROPERTY_BOOL(IsTimeLimit); //是否有时间限定
	DECLARE_PROPERTY_OBJECT(OperateActTime, ActTime); //时间限定
	DECLARE_PROPERTY_OBJECT(vecWelfareItem, VecWelfareItem);
};

//七日目标活动
struct Goals7DaysData
{
	uint32_t status;			//当前状态1,代表未达成，2代表完成未领取，3代表已领取
	int32_t completeValue;     //完成数
	const TargetTargetCfgInfo *pCfg;
	Goals7DaysData() :status(1), completeValue(0), pCfg(nullptr){}
};
typedef std::map<uint32_t, Goals7DaysData> MAP_SINGLE_GOALS7DAYS;
typedef std::map<uint32_t, MAP_SINGLE_GOALS7DAYS> MAP_ALL_GOALS7DAYS;

enum
{
	OPERATE_WELFARE_ACTICE_START_TIMER_ZHUANPAN = 1,
	OPERATE_WELFARE_ACTICE_END_TIMER_ZHUANPAN = 2,
	OPERATE_WELFARE_ACTICE_START_TIMER_REPEATCOST = 3,
	OPERATE_WELFARE_ACTICE_END_TIMER_REPEATCOST = 4,
	OPERATE_WELFARE_ACTICE_START_TIMER_ZHANLI = 5,
	OPERATE_WELFARE_ACTICE_END_TIMER_ZHANLI = 6,
	OPERATE_WELFARE_ACTICE_START_TIMER_LEVEL = 7,
	OPERATE_WELFARE_ACTICE_END_TIMER_LEVEL = 8,
};

class ExchangeItemCfg
{
public:
	DECLARE_PROPERTY_UINT32(PropId);
	DECLARE_PROPERTY_UINT32(PropNum);
};

typedef std::vector<ExchangeItemCfg> VecExchangeItemCfg;

class ExchangeRewardCfg
{
public:
	DECLARE_PROPERTY_UINT32(IndexId);
	DECLARE_PROPERTY_UINT32(RewardPropId);
	DECLARE_PROPERTY_UINT32(RewardPropNum);
	DECLARE_PROPERTY_UINT32(MaxUseCount);
	DECLARE_PROPERTY_OBJECT(VecExchangeItemCfg, VecExchangeItemCfg);
};

class OperateWelfareMgr : public ITimerHandler, public ManualSingleton<OperateWelfareMgr>
{
	friend class ManualSingleton<OperateWelfareMgr>;
public:
	bool Init();
	bool UnInit();
	bool Update(uint64_t tick);
	void OnTimer(uint32_t nTimerID);
public:
	typedef std::map<uint32_t, OperateActConfigItem*> WelfareConfigMap;
	typedef std::map<uint32_t, ExchangeRewardCfg> MapExchangeRewardCfg;
	typedef std::map<uint32_t, std::pair<uint32_t, uint32_t>> MapRechargeIdActIdItemId;	//充值ID对应的奖励ID
	typedef std::set<uint32_t> SetSpecialExchangeItems; //特殊兑换，需要被用来兑换的物品ID，用来提高事件性能
	DECLARE_PROPERTY_UINT32(Goal7Daysprogress);		// 七日目标总进度
	DECLARE_PROPERTY_UINT32(Goal7DaysDisount);		// 七日目标折扣
private:
	DECLARE_PROPERTY_OBJECT(WelfareConfigMap, ConfigMap);
	DECLARE_PROPERTY_OBJECT(MAP_ALL_GOALS7DAYS, Goal7DaysMap);
	DECLARE_PROPERTY_OBJECT(MapExchangeRewardCfg, MapExchangeRewardCfg);
	DECLARE_PROPERTY_OBJECT(MapRechargeIdActIdItemId, MapRechargeIdActIdItemId);
	DECLARE_PROPERTY_OBJECT(SetSpecialExchangeItems, SetSpecialExchangeItems)
public:
	const OperateActConfigItem* GetWelfareConfigItem(uint32_t ActId);
	OperateActConfigItem* GetWelfareConfigItemFromActType(uint32_t ActType);
	const WelfareConfigItem* GetWelfareConfigItem(uint32_t ActId, uint32_t ItemId);
	std::pair<uint32_t, uint32_t>* GetActIdItemIdFromRechargeId(uint32_t rechargeId);
	const WelfareConfigMap* GetWelareConfig() { return &mConfigMap; }
	const MAP_ALL_GOALS7DAYS& GetGoal7DaysData() const { return mGoal7DaysMap; }
	const MAP_SINGLE_GOALS7DAYS* GetSingleGoals7Data(uint32_t targetType) const;
	const MapExchangeRewardCfg& GetExchangeRewardMap() const { return mMapExchangeRewardCfg; }
	const ExchangeRewardCfg* GetExchangeRewardConfig(uint32_t indexId) const;
	void AddSpecialExchangeItemId(uint32_t itemId) { mSetSpecialExchangeItems.insert(itemId); }
	bool IsSpecialExchangeItemId(uint32_t itemId) const;
};

#define g_MakeOperateWelfareMgr() (OperateWelfareMgr::Instance())
#define g_GetOperateWelfareMgr()  (OperateWelfareMgr::GetInstance())
#define g_DelOperateWelfareMgr()  (OperateWelfareMgr::Destroy())

